package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.produce.domain.req.MachiningReq;
import com.ruibang.glass.produce.entity.JqCutCompensate;
import com.ruibang.glass.produce.feign.QualityApi;
import com.ruibang.glass.produce.mapper.CutCompensateMapper;
import com.ruibang.glass.produce.service.JqCutCompensateService;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import com.teaming.cloud.framework2.common.model.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-01-26  10:06
 * @Description: TODO
 * @Version: 1.0
 */

@Slf4j
@Service
public class JqCutCompensateServiceImpl extends ServiceImpl<CutCompensateMapper, JqCutCompensate> implements JqCutCompensateService {

    @Autowired
    private QualityApi qualityApi;

    @Override
    public IPage<JqCutCompensate> pageQuery(MachiningReq machiningReq) {
        long end = machiningReq.getEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long start = machiningReq.getStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        final IPage<JqCutCompensate> page = new Page<>(machiningReq.getPageNo(), machiningReq.getPageSize());
        IPage<JqCutCompensate> iPage = baseMapper.pageQuery(page, start, end, machiningReq.getGlassId());
        if (iPage != null && CollectionUtils.isNotEmpty(iPage.getRecords())) {
            List<JqCutCompensate> data = iPage.getRecords();
            List<String> ids = data.stream().map(JqCutCompensate::getGlassId).collect(Collectors.toList());
            ResultBody<Map<String, String>> resultBody = qualityApi.getColdIdMappingHotId(ids);
            if (resultBody != null && resultBody.getData() != null && resultBody.getData().size() > 0) {
                Map<String, String> map = resultBody.getData();
                for (JqCutCompensate jqCutCompensate : data) {
                    jqCutCompensate.setHotGlassId(map.get(jqCutCompensate.getGlassId()));
                }
            }
        }
        return iPage;
    }

    @Override
    public void export(MachiningReq machiningReq, HttpServletResponse response) {
        machiningReq.setPageNo(-1);
        machiningReq.setPageSize(-1);
        IPage<JqCutCompensate> page = this.pageQuery(machiningReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<JqCutCompensate> jqCutCompensateList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), JqCutCompensate.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("精切补偿值数据").doWrite(jqCutCompensateList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }
}
